/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package asa;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author scriptoff
 */
public class Variables {

    private static Variables v;
    private HashMap<ASAInst, HashMap<String, Variable>> local;
    private HashMap<String, Variable> global;

    private Variables() {
        local = new HashMap<ASAInst, HashMap<String, Variable>>();
        global = new HashMap<String, Variable>();
        
    }

    public static Variables getInstance() {
        if (v == null) {
            v = new Variables();
        }
        return v;
    }

    public Integer getValue(ASAInst bl, String key) {
        Integer ret = null;
        try {
            Variable v = local.get(bl).get(key);
            ret = v.getValeur();
            //System.out.print("Debug getValue("+key+" " + bl + ") : ");
        } catch (NullPointerException ex) {
            Variable v = global.get(key);
            if (v != null){
                ret = v.getValeur();
                //System.out.print("Debug getValue(@"+key+", " + bl + ") : ");
            }
        }
        if(ret == null){
            ret = 0;
           //System.out.print("Debug getValue("+key+", " + bl + ") : ");
        }
        //System.out.println(ret);
        return ret;
    }
 
    public Integer getValue(ASAInst bl, String key, List<Integer> l) {
        Integer ret = null;
        try {
            Variable v = local.get(bl).get(key);
            ret = v.getValeur(l);
            //System.out.print("Debug getValue("+key+" " + bl + ") : ");
        } catch (NullPointerException ex) {
            Variable v = global.get(key);
            if (v != null){
                ret = v.getValeur(l);
                //System.out.print("Debug getValue(@"+key+", " + bl + ") : ");
            }
        }
        /*if(ret == null){
            ret = 0;
           //System.out.print("Debug getValue("+key+", " + bl + ") : ");
        }*/
        //System.out.println(ret);
        return ret;
    }

    public void setValue(ASAInst bl, String key, int value) {
        if (bl != null) {
            HashMap<String, Variable> hB = local.get(bl);
            if (hB == null) {
                hB = new HashMap<String, Variable>();
                local.put(bl, hB);
            }
            hB.put(key, new Variable(value));
            //System.out.println("Debug setValue("+key+", " + bl + ") : "+value);
        }
        else {
            global.put(key, new Variable(value));
            //System.out.println("Debug setValue(@"+key+", " + bl + ") : "+value);
        }
    }

    public void setValue(ASAInst bl, String key, List<Integer> indexList, int value) {
        Variable v  = null;
        if(bl != null){
            HashMap<String, Variable> hB = local.get(bl);
            if(hB != null && hB.containsKey(key))
                v = hB.get(key);
            else 
                v = new Variable();
        }
        else {
            if(global.containsKey(key))
                v = global.get(key);
            else 
                v = new Variable();
        }
        v.setValeur(indexList, value);
        if (bl != null) {
            HashMap<String, Variable> hB = local.get(bl);
            if (hB == null) {
                hB = new HashMap<String, Variable>();
                local.put(bl, hB);
            }
            if(!hB.containsKey(key))
                hB.put(key, v);
        }
        else {
            if(!global.containsKey(key))
                global.put(key, v);
        }
    }

    public void deleteLocal(ASABloc bloc) {
        local.remove(bloc);
    }
}
